
export const JSONPATH_JOIN_CHAR = '.';

export const SCHEMA_TYPE = [
  'string',
  'number',
  'array',
  'object',
  'boolean',
  'integer'
];
export const defaultInitSchemaData = {
  type: 'object',
  title: 'title',
  properties: {
    root: {
      type: 'object',
      properties: {},
      disabled: true,
    }
  }
};
export const defaultSchema = {
  string: {
    type: 'string'
  },
  number: {
    type: 'number'
  },
  array: {
    type: 'array',
    items: {
      type: 'string'
    }
  },
  object: {
    type: 'object',
    properties: {}
  },
  boolean: {
    type: 'boolean'
  },
  integer: {
    type: 'integer'
  }
};

export const uuid = () => {
  return Math.random()
    .toString(16)
    .substr(2, 5);
};

// 二维数组扁平化, [{ value: [] }, 1 , 2]
export const flat = (arr) => {
  return arr.reduce((cur, next) => {
    if (Array.isArray(next.value)) {
      cur = [...next.value, ...cur];
    } else {
      cur.push(next);
    }
    return cur;
  }, []);
}

export const deleteData = (state, keys) => {
  let curState = state;
  for (let i = 0; i < keys.length - 1; i++) {
    curState = curState[keys[i]];
  }

  delete curState[keys[keys.length - 1]];
};
